import Taro, {Component, Config} from '@tarojs/taro'
import { View } from '@tarojs/components'
import './index.scss'
import { getCommentPage } from '@/api/commentApi'
import { showEmpty } from "@/utils/utils"
import { Loading, LoadMore, EmptyData } from '@/components/index'
import CommentItem from '@/components/comment/comment-item/index'

export default class Index extends Component {
  state: any
  config: Config = {
    backgroundTextStyle: "dark",
    navigationBarTitleText: '商品评价',
  }

  constructor() {
    super(...arguments)
    this.state = {
      page: 1,
      limit: 10,
      loading: true,
      dataList: [],
      loadStatus: 'loading',
    }
  }

  componentWillMount() {
    this.getPage()
  }

  //  上拉加载
  onReachBottom() {
    this.getPage()
  }

  //  获取商品评论
  getPage() {
    const { params } = this.$router
    const { page, limit, dataList, loadStatus } = this.state
    if(loadStatus === 'noMore') {
      return
    }

    getCommentPage({ page, limit: 10, productId: params.id }).then(res => {
      const { list, totalCount } = res.data
      this.setState({
        page: page+1,
        loading: false,
        dataList: page === 1 ? list : dataList.concat(list),
        loadStatus: (page * limit >= totalCount) ? 'noMore' : 'loading',
      })
    })
  }

  render() {
    const { loading, dataList, loadStatus } = this.state
    return (<View className='comment-page'>
      {loading && <Loading isCover/>}
      <View className='comment-list'>
        {dataList.map(item => {
          return (<CommentItem key={item.id} info={item} className='comment-box'/>)
        })}
      </View>
      {!showEmpty(dataList, loadStatus) && <LoadMore status={loadStatus}/>}
      {showEmpty(dataList, loadStatus) && <View className='list-empty h-p-100'><EmptyData/></View>}
    </View>)
  }
}
